home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / intrlib1.zip / SLCT_CLR.C < prev    next >
C/C++ Source or Header  |  1992-02-20  |  7KB  |  221 lines

  1. /******************************************************************************
  2. * Select the colors to be used according to specified device.              *
  3. *                                          *
  4. *                    Written by Gershon Elber,  Oct. 1990  *
  5. *******************************************************************************
  6. * History:                                      *
  7. *  3 Oct 90 - Version 1.0 by Gershon Elber.                      *
  8. ******************************************************************************/
  9.  
  10. #include "intr_loc.h"
  11. #include "intr_gr.h"
  12.  
  13. #define MAX_COLORS (INTR_NUM_COLORS * 4)
  14.  
  15. static IntrBType
  16. #ifdef DJGCC
  17.     Allow256 = TRUE;
  18. #else
  19.     Allow256 = FALSE;
  20. #endif /* DJGCC */
  21. static int
  22.     InterColorTable[MAX_COLORS],
  23.     NumOfIntensities = 1;
  24.  
  25. /******************************************************************************
  26. * Given the device, reset the color table used by this library. Each color in *
  27. * InterColorType has up to 4 intensity levels that may be used. If the system *
  28. * does not enough colors BLACK color is substituted instead. Only the highest *
  29. * intensity of the four is guarantee to be non BLACK.                  *
  30. * This routine should be called once as part of initialization.              *
  31. ******************************************************************************/
  32. void _IntrAllocColors(void)
  33. {
  34.     int i, j, k, l, Red, Green, Blue, Index, UseColors;
  35.  
  36.     if (GRScreenMaxColors == 256 && !Allow256)
  37.     UseColors = 16;
  38.     else
  39.     UseColors = GRScreenMaxColors;
  40.  
  41.     switch (UseColors) {
  42.     case 2:
  43.         /* Its a monocrome system. Allocate highest intensity as white   */
  44.         /* and all others as black. This is not going to look good...    */
  45.     case 4:
  46.         /* Its probably a monocrome system with gray levels.         */
  47.         for (i = 0; i < INTR_NUM_COLORS; i++) {
  48.         j = l = (i << 2);
  49.         k = j + 4;
  50.         for ( ; j < k ; j++ )
  51.             InterColorTable[j] = j == l;
  52.         }
  53.         NumOfIntensities = 1;
  54.         break;
  55.     case 16:
  56.         /* Its an assumed to be EGA/VGA system and the default EGA/VGA   */
  57.         /* Pallette is assumed as well as follows:                 */
  58.         /* {   0,   0,   0 },   Color 0. Black                  */
  59.         /* {   0,   0, 170 },   Color 1. Blue                  */
  60.         /* {   0, 170,   0 },   Color 2. Green                  */
  61.         /* {   0, 170, 170 },   Color 3. Cyan                  */
  62.         /* { 170,   0,   0 },   Color 4. Red                  */
  63.         /* { 170,   0, 170 },   Color 5. Magenta                  */
  64.         /* { 170, 170,   0 },   Color 6. Brown                  */
  65.         /* { 170, 170, 170 },   Color 7. LightGray                  */
  66.         /* {  85,  85,  85 },   Color 8. DarkGray                  */
  67.         /* {  85,  85, 255 },   Color 9. LightBlue                  */
  68.         /* {  85, 255,  85 },   Color 10. LightGreen              */
  69.         /* {  85, 255, 255 },   Color 11. LightCyan              */
  70.         /* { 255,  85,  85 },   Color 12. LightRed                  */
  71.         /* { 255,  85, 255 },   Color 13. LightMagenta              */
  72.         /* { 255, 255,  85 },   Color 14. Yellow                  */
  73.         /* { 255, 255, 255 },   Color 15. White                  */
  74.         /* At this time I prefer not to change the pallette to ensure    */
  75.             /* these colors are in fact used as user may attempt and use     */
  76.             /* color directly.                             */
  77.         /* Only two intensities are assumed for each color although the  */
  78.             /* third is set to be thsame as the second...             */
  79.             for (i = 0; i < INTR_NUM_COLORS; i++) {
  80.         j = (i << 2);
  81.                 InterColorTable[j + 3] = 0;
  82.         switch (i) {
  83.             case INTR_COLOR_WHITE:
  84.             InterColorTable[j] = 15;
  85.             InterColorTable[j + 2] = InterColorTable[j + 1] = 7;
  86.             break;
  87.             case INTR_COLOR_BLACK:
  88.             InterColorTable[j] = 0;
  89.             InterColorTable[j + 2] = InterColorTable[j + 1] = 0;
  90.             break;
  91.             case INTR_COLOR_RED:
  92.             InterColorTable[j] = 12;
  93.             InterColorTable[j + 2] = InterColorTable[j + 1] = 4;
  94.             break;
  95.             case INTR_COLOR_GREEN:
  96.             InterColorTable[j] = 10;
  97.             InterColorTable[j + 2] = InterColorTable[j + 1] = 2;
  98.             break;
  99.             case INTR_COLOR_BLUE:
  100.             InterColorTable[j] = 9;
  101.             InterColorTable[j + 2] = InterColorTable[j + 1] = 1;
  102.             break;
  103.             case INTR_COLOR_YELLOW:
  104.             InterColorTable[j] = 14;
  105.             InterColorTable[j + 2] = InterColorTable[j + 1] = 6;
  106.             break;
  107.             case INTR_COLOR_CYAN:
  108.             InterColorTable[j] = 11;
  109.             InterColorTable[j + 2] = InterColorTable[j + 1] = 3;
  110.             break;
  111.             case INTR_COLOR_MAGENTA:
  112.             InterColorTable[j] = 13;
  113.             InterColorTable[j + 2] = InterColorTable[j + 1] = 5;
  114.             break;
  115.         }
  116.             }
  117.  
  118.         /* Init. to WHITE. */
  119.             for (Index = 16; Index < GRScreenMaxColors; )
  120.         GRSetRGBPalette(Index++, 255, 255, 255);
  121.  
  122.         NumOfIntensities = 3;
  123.         break;
  124.     case 256:
  125.         /* Wow. This is going to be fun. We can actually allocate our    */
  126.         /* own colors, so 4 levels are allocated for each color.         */
  127.         Index = 16;           /* Start allocating colors from Index 16. */
  128.             for (i = 0; i < INTR_NUM_COLORS; i++) {
  129.         j = (i << 2);
  130.         Red = Green = Blue = 0;
  131.         switch (i) {
  132.             case INTR_COLOR_WHITE:
  133.             Red = Green = Blue = 63;
  134.             break;
  135.             case INTR_COLOR_BLACK:
  136.             break;
  137.             case INTR_COLOR_RED:
  138.             Red = 63;
  139.             break;
  140.             case INTR_COLOR_GREEN:
  141.             Green = 63;
  142.             break;
  143.             case INTR_COLOR_BLUE:
  144.             Blue = 63;
  145.             break;
  146.             case INTR_COLOR_YELLOW:
  147.             Red = Green = 63;
  148.             break;
  149.             case INTR_COLOR_CYAN:
  150.             Green = Blue = 63;
  151.             break;
  152.             case INTR_COLOR_MAGENTA:
  153.             Red = Blue = 63;
  154.             break;
  155.         }
  156.  
  157.         for (k = 4; k > 0; k--) {
  158. #ifdef DJGCC
  159.             GRSetRGBPalette(Index, Red * k, Green * k, Blue * k);
  160. #else
  161.             GRSetRGBPalette(Index,
  162.                                 (Red * k) >> 2,
  163.                                     (Green * k) >> 2,
  164.                                     (Blue * k) >> 2);
  165. #endif /* DJGCC */
  166.             InterColorTable[j + 4 - k] = Index++;
  167.         }
  168.             }
  169.  
  170.         while (Index < 256)
  171.         GRSetRGBPalette(Index++, 255, 255, 255);  /* Init. to WHITE. */
  172.  
  173.         NumOfIntensities = 4;
  174.         break;
  175.         default:
  176.             IntrFatalError("Undefined number of colors in device.");
  177.             break;
  178.     }
  179. }
  180.  
  181. /******************************************************************************
  182. * Allow 256 colors which enable 4 shades of each color.                  *
  183. * This mode is incompatible with the EGA/VGA 16 colors and colors will look   *
  184. * somewhat different in this mode.                          *
  185. ******************************************************************************/
  186. void IntrAllow256Colors(IntrBType Allow256Colors)
  187. {
  188. #ifndef DJGCC
  189.     Allow256 = Allow256Colors;
  190. #endif /* DJGCC */
  191. }
  192.  
  193. /******************************************************************************
  194. * Select a color with intensity.                          *
  195. ******************************************************************************/
  196. int IntrAllocColor(IntrColorType Color, IntrIntensityType Intensity)
  197. {
  198.     int SelectedIntensity, GRColor;
  199.  
  200.     switch (Intensity) {
  201.     case INTR_INTENSITY_VHIGH:
  202.         SelectedIntensity = 0;
  203.             break;
  204.     case INTR_INTENSITY_HIGH:
  205.         SelectedIntensity = 1;
  206.             break;
  207.     case INTR_INTENSITY_LOW:
  208.         SelectedIntensity = 2;
  209.             break;
  210.     case INTR_INTENSITY_VLOW:
  211.         SelectedIntensity = 3;
  212.             break;
  213.     }
  214.  
  215.     GRColor = InterColorTable[(((int) Color) << 2) + SelectedIntensity];
  216.     GRSetColor(GRColor);
  217.     GRSetFillStyle(INTR_FILL_SOLID, GRColor);
  218.  
  219.     return GRColor;
  220. }
  221.